package nadav.tasher.lightool.graphics.view;

import nadav.tasher.lightool.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;

public class TableView extends DirectionalLayout {
    public static final int MODE_RW = 0;
    public static final int MODE_RO = 1;
    public static final int MODE_VO = 2;
    private ZSONArray currentData;
    private String[] titles;
    private int mode, size;
    private Color textcolor;
    private OnChanged onChanged;
    private int removeButtonSize;
    private boolean removeButton = false;

    public TableView(Context context) {
        super(context);
    }

    public TableView(Context context, Color textcolor, int size, int mode, ZSONArray currentData, String[] titles) {
        super(context);
        this.currentData = currentData;
        this.mode = mode;
        this.size = size;
        this.textcolor = textcolor;
        this.titles = titles;
        init();
    }

    public void setOnChanged(OnChanged onChanged) {
        this.onChanged = onChanged;
    }

    public void setShowRemoveButton(boolean isShown, int buttonSize) {
        removeButton = isShown;
        removeButtonSize = buttonSize;
        init();
    }

    public ZSONArray getCurrentData() {
        return currentData;
    }

    private void init() {
        removeAllComponents();
        setOrientation(VERTICAL);
        setAlignment(LayoutAlignment.CENTER);
        TableViewRow title = new TableViewRow(getContext(), textcolor, size, MODE_VO, titles, titles);
        addComponent(title);
        ScrollView rowScroll = new ScrollView(getContext());
        final DirectionalLayout rows = new DirectionalLayout(getContext());
        rows.setOrientation(DirectionalLayout.VERTICAL);
        rows.setAlignment(LayoutAlignment.CENTER);
        rowScroll.addComponent(rows);
        addComponent(rowScroll);
        addListElements(rows);
        Button button = new Button(getContext());
//        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
//        layoutConfig.setMargins(0, 50, 0, 0);
//        button.setText("测试按钮");
//        button.setTextSize(50);
        VectorElement element = new VectorElement(getContext(), ResourceTable.Graphic_ic_add);
        button.setBackground(element);
        button.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                currentData.add(new ZSONObject());
                addListElements(rows);
                if (onChanged != null) onChanged.onChanged(currentData);
            }
        });
        if (mode == MODE_RW)
            addComponent(button);
    }

    private void addListElements(final DirectionalLayout list) {
        list.removeAllComponents();
        for (int t = 0; t < currentData.size(); t++) {
            String[] current = new String[titles.length];
            ZSONObject obj = currentData.getZSONObject(t);
            for (int a = 0; a < titles.length; a++) {
                if (obj.containsKey(titles[a].toLowerCase())) {
                    current[a] = obj.getString(titles[a].toLowerCase());
                }
            }
            final int finalT = t;
            final TableViewRow row = new TableViewRow(getContext(), textcolor, size, mode, current, titles);
            if (removeButton) {
                row.showRemoveButton(new ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        currentData.remove(finalT);
                        addListElements(list);
                        if (onChanged != null) onChanged.onChanged(currentData);
                    }
                }, removeButtonSize);
            }
            list.addComponent(row);
            row.setOnChanged(new TableViewRow.OnChanged() {
                @Override
                public void onChanged(String name, String value) {
                    ZSONObject buildNew=new ZSONObject();
                    for (int b=0;b<titles.length;b++){
                        buildNew.put(titles[b].toLowerCase(), row.getCurrentValues()[b]);
                    }
                    currentData.add(finalT,buildNew);
                    if (onChanged!=null) onChanged.onChanged(currentData);
                }
            });

        }
    }


    public interface OnChanged {
        void onChanged(ZSONArray nowData);
    }

    static class TableViewRow extends DirectionalLayout {
        int mode;
        int size;
        Color textcolor;
        String[] currentValues;
        String[] hints;
        OnChanged onChanged;
        Button remove;

        public TableViewRow(Context context) {
            super(context);
        }

        public TableViewRow(Context context, Color textcolor, int size, int mode, String[] values, String[] hints) {
            super(context);
            this.mode = mode;
            currentValues = values;
            this.hints = hints;
            this.size = size;
            this.textcolor = textcolor;
            init();
        }

        public void setOnChanged(OnChanged onChanged) {
            this.onChanged = onChanged;
        }

        public String[] getCurrentValues() {
            return currentValues;
        }

        public void showRemoveButton(Component.ClickedListener todo, int s) {
            remove.setLayoutConfig(new LayoutConfig(s, ComponentContainer.LayoutConfig.MATCH_PARENT));
            int c = 1;
            getComponentAt(c).setLayoutConfig(new LayoutConfig(getComponentAt(c).getLayoutConfig().width - remove.getLayoutConfig().width, getComponentAt(c).getLayoutConfig().height));
            remove.setVisibility(Component.VISIBLE);
            remove.setClickedListener(todo);
        }

        private void init() {
            setOrientation(HORIZONTAL);
            setAlignment(LayoutAlignment.CENTER);
            remove = new Button(getContext());
//            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
//            layoutConfig.setMargins(0, 50, 0, 0);
//            remove.setText("测试按钮2");
//            remove.setTextSize(50);
            VectorElement element = new VectorElement(getContext(),ResourceTable.Graphic_ic_delete);
            remove.setBackground(element);
//            remove.setPadding(10, 10, 10, 10);
            addComponent(remove);
            remove.setVisibility(Component.FOCUS_ENABLE);
            for (int i = 0; i < currentValues.length; i++) {
                final Text ctv;
                if (mode == TableView.MODE_VO) {
                    ctv = new Text(getContext());
                } else if (mode == TableView.MODE_RO) {
                    ctv = new Text(getContext());
                    ctv.setTextInputType(0);
                    ctv.setSelected(true);
                } else {
                    ctv = new Text(getContext());
                }
                ctv.setTextColor(textcolor);
                if (hints.length == currentValues.length) {
                    ctv.setHint(hints[i]);
                }
                ctv.setTextSize(20);
                ctv.setText(currentValues[i]);
                final int finalI = i;
                ctv.addTextObserver(new Text.TextObserver() {
                    @Override
                    public void onTextUpdated(String s, int i, int i1, int i2) {
                        EventRunner eventRunner = EventRunner.getMainEventRunner();
                        EventHandler handler = new EventHandler(eventRunner);
                        handler.postTimingTask(new Runnable() {
                            @Override
                            public void run() {
                                if (s.toString().equals(ctv.getText().toString())) {
                                    currentValues[finalI] = s.toString();
                                    if (onChanged != null && hints.length == currentValues.length) {
                                        onChanged.onChanged(hints[finalI], currentValues[finalI]);
                                    }
                                }
                            }
                        }, 100);
                    }
                });
                ctv.setLayoutConfig(new LayoutConfig(size, ComponentContainer.LayoutConfig.MATCH_CONTENT));
                ctv.setTextAlignment(LayoutAlignment.CENTER);
                addComponent(ctv);
            }
        }

        interface OnChanged {
            void onChanged(String name, String value);
        }
    }
}
